export interface IEventListener {
  dispose(): void;
}

export class EventEmitter<T = unknown> {
  #listeners: ((eventData?: T) => void)[] = [];

  listen(listener: (eventData?: T) => void): IEventListener {
    const wrapper = (eventData?: T) => listener(eventData);

    this.#listeners.push(wrapper);

    return {
      dispose: () => {
        this.#listeners = this.#listeners.filter((cb) => cb !== wrapper);
      },
    };
  }

  emit(eventData?: T) {
    this.#listeners.forEach((listener) => listener(eventData));
  }
}
